<?php
class TransportController extends AppController {

	public $name = 'transport';

	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','tedit');

	}

	public function tedit($do='') {	
        //获得地区列表	
		$query = array(
				'fields' => array('area.*'),
				'conditions' => array('area.area_type' => 1),
				);	
		$area_list = POP::exe('[@@:UM area][@@:QU find:all]',$query);
		
		$query = array(
				'fields' => array('transport.*'),
				'conditions' => array('transport.ifopen' => 1),
				);	
		$arr_list = POP::exe('[@@:UM transport][@@:QU find:all]',$query);

	    //循环查出开启的配送方式中，保存的相应的方式	
	    if($do=='do') {
	        $post = $_POST;
		    $id = $post['id'];
	        $arr = array();
	        $info['transport_name']=$post['transport_name'];
	        $info['description'] = $post['description'];
	        foreach ($post['transport_type'] as $k=>$v){
		        $arr[$v]=array();
	        }	
			$tran_list = $arr_list;
	        //循环获得前台传来的值，通过tran_type字段作为数组下标
			foreach ($tran_list as $val){
				$val = $val['transport'];
				$arr[$val['id']]['frist']=$post['frist'.$val['id']];
				$arr[$val['id']]['second']=$post['second'.$val['id']];
				if(isset($post['ord_item_dest'.$val['id']])){
					foreach ($post['ord_item_dest'.$val['id']] as $k=>$v){
						$v = substr($v,0,-1);
						$areaarr = explode(",",$v);
						foreach ($areaarr as $key=>$value){
							$arr[$val['id']][$value]=array("frist"=>$post['ord_area_frist'.$val['id']][$k],"second"=>$post['ord_area_second'.$val['id']][$k]);
						}
					}
				}
			}

			$info['shop_id']= $this->my->id;
			$info['content'] = serialize($arr);					
			$update_items['goodstransport'] = $info;
			
			$query  = array();					
			$query['keyid'] = $id;				
			$query['set'] = $update_items;							
            if(POP::exe('[@@:UM goodstransport][@@:QU save]',$query) ) {			
		        $this->flash("提交成功！", Router::url('goods/ulist/'));
	        }else{
		        $this->flash("提交失败！", Router::url('goods/ulist/'));
	        }			
			
			
			
	    }
		
	    $id = intval(Request::get_args("id"));
		$query = array(
				'fields' => array('goodstransport.*'),
				'conditions' => array('goodstransport.id' => $id),
				);	
        $transport_template_info = POP::exe('[@@:UM goodstransport][@@:QU find:all]',$query);		

        $transport_template_info = $transport_template_info['goodstransport'] ;
	    $transport_type = unserialize($transport_template_info['content']);
	    foreach ($transport_type as $value){
		    $transport_type[]=$value;
	    }
	    $arr = unserialize($transport_template_info['content']);
	    $type_list = array();
	    foreach ($arr_list as $v){
		    $v = $v['transport'];
			$v_tranid = $v['id'];
		    $tran_type=$v['id'];
		    $type_list[$v_tranid]="";	//注:该变量根据$v['id']的值的改变而变，通过循环定义
		    if (isset($arr[$tran_type])&&count($arr[$tran_type])>1) {
			    $emsarea=array();
			    foreach ($arr[$tran_type] as $key=>$value){
				    if(is_numeric($key)){
					    $emsarea["{$value['frist']},{$value['second']}"][]=$key;
				    }
			    }
			    $idlist="";
			    $area_name_list="";
			    $num=0;		
			    foreach ($emsarea as $key=>$value){
				    foreach ($value as $k=>$v){
					    $idlist.=$v.",";
						$query = array(
								'fields' => array('area.*'),
								'conditions' => array('area.id' => $v),
								);	
						$area_info = POP::exe('[@@:UM area][@@:QU find:first]',$query);						
						$area_info = $area_info['area'];
					    $area_name_list.=$area_info['area_name'].",";
				    }
				    $num++;
				    $key_str = explode(",",$key);
				    $type_list[$v_tranid].="&nbsp;&nbsp;&nbsp;&nbsp;<li>至<input type='text' name='ord_item_word'.$tran_type.'[]' value='$area_name_list' onclick=\"eidtarea('$tran_type','$idlist','$num')\" id='item$num'  />
					<input type='hidden' id='itemvalue$num' name='ord_item_dest'.$tran_type.'[]' value='$idlist' />的";
				    $type_list[$v_tranid].="默认运费：";
				    $type_list[$v_tranid].="<input type='text' name='ord_area_frist'.$tran_type.'[]' value='{$key_str[0]}' />"."每超过一件需要增加运费：".
					"<input type='text' name='ord_area_second'.$tran_type.'[]' value='{$key_str[1]}' /></li>";
				    $idlist="";
				    $area_name_list="";					
					
			    }
		    }
	    }		
	    //循环生成json
	    $post=array();
	    foreach ($arr_list as $v){
		    $v = $v['transport'];
		    if(isset($transport_type[$v['id']])){
			    $post[$v['id']]=json_encode($transport_type[$v['id']]);
		    }
	    }
	    $this->set('area_list',$area_list);		
	    $this->set('arr_list',$arr_list);	
	    $this->set('type_list',$type_list);	
	    $this->set('post',$post);	
	    $this->set('transport_template_info',$transport_template_info);	
	    $this->set('id',$id);			

	}
	
	public function index($do='') {
		$query = array(
				'fields' => array('transport.*'),
				'conditions' => array('transport.ifopen' => 1),
				);	
		$tran_list = POP::exe('[@@:UM transport][@@:QU find:all]',$query);	

	    if($do=='do') {
	        $post = $_POST;
	        if (!isset($post['transport_type'])) {
				$this->flash("至少选择一种配送方式！", Router::url('goods/ulist/'));
	        }
	        $arr = array();
	        $info['transport_name']=$post['transport_name'];
	        $info['description'] = $post['description'];
	        foreach ($post['transport_type'] as $k=>$v){
		        $arr[$v]=array();
	        }
	        //循环获得前台传来的值，通过id字段作为数组下标
	        foreach ($tran_list as $val){
			    $val = $val['transport'];
		        $arr[$val['id']]['frist']=$post['frist'.$val['id']];
		        $arr[$val['id']]['second']=$post['second'.$val['id']];
		        if(isset($post['ord_item_dest'.$val['id']])){
			        foreach ($post['ord_item_dest'.$val['id']] as $k=>$v){
				        $v = substr($v,0,-1);
				        $areaarr = explode(",",$v);
				        foreach ($areaarr as $key=>$value){
					        $arr[$val['id']][$value]=array("frist"=>$post['ord_area_frist'.$val['id']][$k],"second"=>$post['ord_area_second'.$val['id']][$k]);
				        }
			        }
		        }
	        }
			$info['shop_id']= $this->my->id;
	        $info['content'] = serialize($arr);		
			$update_items['goodstransport'] = $info;		    		
	        if (POP::exe('[@@:UM goodstransport][@@:QU save]',$update_items)) {
		        $this->flash("提交成功！", Router::url('goods/ulist/'));
	        }else{
		        $this->flash("提交失败！", Router::url('goods/ulist/'));
	        }		
		} else {
            //获得地区列表				
			$query = array(
					'fields' => array('area.*'),
					'conditions' => array('area.area_type' => 1),
					);	
			$area_list = POP::exe('[@@:UM area][@@:QU find:all]',$query);			
	        $this->set('area_list',$area_list);		
	        $this->set('arr_list',$tran_list);
        }		
	}
	
}
?>